package com.example.store;

import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.example.store.dao.ProductDao;
import com.example.store.entity.Product;

import java.util.List;

public class MainActivity extends AppCompatActivity {

    private EditText      name;
    private EditText      balance;
    private ListView      lv;
    private ProductDao    productDao;
    private List<Product> productList;
    private MyAdapter     myAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        productDao = new ProductDao(getApplicationContext());
        name = findViewById(R.id.et_product_name);
        balance = findViewById(R.id.et_product_balance);
        lv = findViewById(R.id.lv);
        productList = productDao.queryAll();
        myAdapter = new MyAdapter();
        lv.setAdapter(myAdapter);

    }

    //增加
    public void add(View v) {
        String Name = name.getText().toString().trim();
        String Balance = balance.getText().toString().trim();
        if (TextUtils.isEmpty(Name) || TextUtils.isEmpty(Balance)) {
            Toast.makeText(getApplicationContext(), "商品名称|金额不能为空", 1).show();
            return;
        } else {
            name.setText(null);
            balance.setText(null);
            productDao.add(Name, Balance);
            List<Product> queryAll = productDao.queryAll();
            productList = queryAll;
            myAdapter.notifyDataSetChanged();
        }
    }

    //数据适配器
    private class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return productList.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view;
            if (convertView == null) {
                view = View.inflate(getApplicationContext(), R.layout.item, null);
            } else {
                view = convertView;
            }
            TextView tv_name = view.findViewById(R.id.nameTV);
            TextView tv_bal = view.findViewById(R.id.balanceTV);
            // TextView tv_id = view.findViewById(R.id.idTV);
            ImageView iv_up = view.findViewById(R.id.upIV);
            ImageView iv_down = view.findViewById(R.id.downIV);
            ImageView iv_delete = view.findViewById(R.id.deleteIV);
            Product product = productList.get(position);
            tv_name.setText(product.getName());
//            tv_id.setText(product.getId());
            tv_bal.setText(product.getBalance());

            iv_delete.setOnClickListener(new View.OnClickListener() {
                @Override
                //删除
                public void onClick(View v) {
                    AlertDialog.Builder bb = new AlertDialog.Builder(MainActivity.this);
                    bb.setTitle("您确定要删除该条信息吗？");
                    bb.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            productList.remove(position);
                            productDao.delete(product.getId());
                            notifyDataSetChanged();
                        }
                    });
                    bb.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                        }
                    });
                    bb.show();
                }
            });

            //增加
            iv_up.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String balance = product.getBalance();
                    int i = Integer.parseInt(balance) + 1;
                    product.setBalance(String.valueOf(i));
                    notifyDataSetChanged();
                    productDao.update(product.getId(), product.getBalance());
                }
            });
            //减少
            iv_down.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int i1 = Integer.parseInt(product.getBalance());
                    if (i1 >= 1) {
                        String balance = product.getBalance();
                        int i = Integer.parseInt(balance) - 1;
                        product.setBalance(String.valueOf(i));
                        notifyDataSetChanged();
                        productDao.update(product.getId(), product.getBalance());
                    }
                }
            });
            return view;
        }


    }


}